{# SPDX-FileCopyrightText: © 2014-2018 Siemens AG

   SPDX-License-Identifier: FSFAP
#}
selectedLicensesTableColumns = [
  {"sTitle": "{{ 'Action'| trans }}<img src='images/info_16.png' data-toggle='tooltip' title='{{ "Current actions are: 1. Cross: remove (or add again) the license from a clearing result. Note that the license finding stays in the table, but it is not considered for a clearing result 2. Star: mark this license as main license as the package. A main license will appear in the listing of the browse view as well in the reporting"|trans }}' alt='' class='info-bullet'/>", "sClass": "center nobreaktext", "bSortable": false, "sWidth" : "1%"},
  {"sTitle": "{{ 'License'| trans }}<img src='images/info_16.png' data-toggle='tooltip' title='{{ "Short ID (or SPDX ID) of the license. Clicking the license short ID will reveal the  license text"|trans }}' alt='' class='info-bullet'/>", "sClass": "left nobreaktext"},
  {"sTitle": "{{ 'Source'| trans }}<img src='images/info_16.png' data-toggle='tooltip' title='{{ "Showing the agent or user action which yielded this finding"|trans }}' alt='' class='info-bullet'/>", "sClass": "left nobreaktext", "bSortable": false},
  {"sTitle": "{{ 'License Text'| trans }}<img src='images/info_16.png' data-toggle='tooltip' title='{{ "The license text will normally appear in the reporting. If the text from the database should be overridden, because the license text is customized, then the custom license text for reporting can be pasted here"|trans }}' alt='' class='info-bullet'/>", "sClass": "left nobreaktext", "bSortable": false},
  {"sTitle": "{{ 'Acknowledgement'| trans }}<img src='images/info_16.png' data-toggle='tooltip' title='{{ "Special acknowledgement text found or determined for a particular license can be placed into the according row of this column"|trans }}' alt='' class='info-bullet'/>", "sClass": "left nobreaktext", "bSortable": false},
  {"sTitle": "{{ 'Comment'| trans }}<img src='images/info_16.png' data-toggle='tooltip' title='{{ "Showing a comment in the reporting for this particular license finding in this file can be shown here. Could be useful for - to be discussed"|trans }}' alt='' class='info-bullet'/>", "sClass": "left nobreaktext", "bSortable": false}
];

uploadId = {{ uploadId }};
itemId = {{ itemId }};

selectedLicensesTableConfig = {
  "bServerSide": true,
  "sAjaxSource": "?mod=conclude-license&do=licenseDecisions",
  "fnServerData": function (sSource, aoData, fnCallback) {
    aoData.push({"name": "upload", "value": uploadId});
    aoData.push({"name": "item", "value": itemId});
    $.getJSON(sSource, aoData, fnCallback)
      .fail(failed)
  },
  "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      if (nRow.className.match(/(^| )removed($| )/g)  ) {
          $('td', nRow).addClass('read_only');
      }
  },
  "aoColumns": selectedLicensesTableColumns,
  "iDisplayLength": 25,
  "bProcessing": true,
  "bStateSave": true,
  "bRetrieve": true,
  "bPaginate": false,
  "bFilter": false,
  "bAutoWidth": false
};

editableConfiguration = {
  "sUpdateURL": "?mod=conclude-license&do=updateClearings",
  "fnOnEditing": function(input) {return input[0].value;},
  "fnOnEdited": function (status){ if(status==="success") { $('#decTypeSet').addClass('border-danger');} } ,
  "fnOnDeleted": function(result) { createTable().fnDraw(false);},
  "sSuccessResponse": "success",
  "aoColumns": [null, null, null, null, null, null]
};

function createClearingTable() {
  otable = $('#licenseDecisionsTable').dataTable(selectedLicensesTableConfig).makeEditable(editableConfiguration);
  return otable;
}

addLicenseTableColumns = [
  {"sTitle": "{{ 'License'| trans }}", "sClass": "left"},
  {"sTitle": "{{ 'Action'| trans }}", "sClass": "center", "bSortable": false}
];

addLicenseTableConfig = {
  "bServerSide": true,
  "sAjaxSource": "?mod=conclude-license&do=licenses",
  "fnServerData": function (sSource, aoData, fnCallback) {
    aoData.push({"name": "upload", "value": uploadId});
    aoData.push({"name": "item", "value": itemId});
    $.getJSON(sSource, aoData, fnCallback).fail(failed);
  },
  "aoColumns": addLicenseTableColumns,
  "iDisplayLength": 25,
  "bProcessing": true,
  "bStateSave": true,
  "bRetrieve": true,
  "bPaginate": false,
  "bFilter": true
};

addClearingHistoryColumns = [
  {"sTitle": "{{ 'Date'| trans }}", "sClass": "left", "bSortable": false},
  {"sTitle": "{{ 'Username'| trans }}", "sClass": "left", "bSortable": false},
  {"sTitle": "{{ 'Scope'| trans }}", "sClass": "left", "bSortable": false},
  {"sTitle": "{{ 'Type'| trans }}", "sClass": "left", "bSortable": false},
  {"sTitle": "{{ 'Licenses'| trans }}", "sClass": "left", "bSortable": false}
];

addClearingHistoryTableConfig = {
  "bServerSide": true,
  "sAjaxSource": "?mod=conclude-license&do=showClearingHistory",
  "fnServerData": function (sSource, aoData, fnCallback) {
    aoData.push({"name": "upload", "value": uploadId});
    aoData.push({"name": "item", "value": itemId});
    $.getJSON(sSource, aoData, fnCallback).fail(failed);
  },
  "aoColumns": addClearingHistoryColumns,
  "iDisplayLength": 25,
  "bProcessing": true,
  "bStateSave": true,
  "bRetrieve": false,
  "bPaginate": false,
  "bFilter": false,
  "bDestroy": true
};


function createLicenseSelectionTable() {
  otable = $('#licenseSelectionTable').dataTable(addLicenseTableConfig);
  return otable;
}

function createClearingHistoryDataTable() {
  otable = $('#ClearingHistoryDataModalTable').dataTable(addClearingHistoryTableConfig);
  return otable;
}
$(document).ready(function () {
  createClearingTable();
  createLicenseSelectionTable();
  $('[data-toggle="tooltip"]').tooltip();
});

